%==========================================================================
%=  This file is part of the Sound Restoration Project
%=  (c) Copyright Industrial Mathematics Institute
%=                University of South Carolina, Department of Mathematics
%=  ALL RIGHTS RESERVED
%=
%=  Author: Borislav Karaivanov
%==========================================================================

%==========================================================================
% List of the files on which this procedure depends:
%
% Any file used by the particular function whose handle is passed in the
% input argument "readFramesFunctHandle". Those may include:
% patientlyKeepTryingToReadUntilErrorFree.m
% readAndCheckVideoFrames.m
% readImageFile.m
% readImagesByIndexRange.m
% readVideoFrames.m 
% readdpx.m
% Image processing toolbox
%
%==========================================================================

%==========================================================================
% REMARK: For the moment this function is simply a placeholder for an
% algorithmic approach to be invented and implemented in the future.
%==========================================================================
% The function "estimateSoundTrackBounds" automatically estimates the left
% and right bounds of the sound track region in a sequence of frames.
% INPUT: "readFramesFunctHandle" is a handle to a function capable of
% reading any range of frames from a preset source.
% "frameRange" is a single integer or a row vector of two integers
% specifying the index of a single frame or a range of frames to be read
% from a preset source.
% "filmStandard" is an optional string specifying the format of the
% original film from which the frames came. This information is used to set
% approximate soundtrack bounds likely to be close to the actual bounds.
% OUTPUT: "leftBound" and "rightBound" return the estimated left and right
% vertical bounds of the sound track region in the given frames.
%==========================================================================
function [leftBound, rightBound] = estimateSoundTrackBounds(...
    readFramesFunctHandle, frameRange, filmStandard) 

if (nargin < 1)
    filmStandard = '';
end

% Specify that read frames are not to be converted to grayscale.
doConvertToGray = false;

% Read in the first frame.
frameArr = readFramesFunctHandle(frameRange(1), doConvertToGray);
 
% Get the frame width in pixels.
frameWidth = size(frameArr, 2);  

% Initialize the soundtrack's left and right bounds. 
switch filmStandard 
    case {'+25 [16 mm]', '16mm'} 
        % Assume 16 mm film.
        leftBound = round((0.83)*frameWidth);
        rightBound = round((0.98)*frameWidth);
        
    case {'+21 [theatrical standard]', '+11 [all Movietone negative]', '-14 [Phonofilm]', }
        % Assume 35 mm film whose sound track is about 2.9 mm wide.
        leftBound = round((3/21.6)*frameWidth);
        rightBound = leftBound + round((3/35)*frameWidth);
        
    otherwise
        % Assume 35 mm film whose sound track is about 2.9 mm wide.
        leftBound = round((3/21.6)*frameWidth);
        rightBound = leftBound + round((3/35)*frameWidth);
end 

return;
% end of the function "estimateSoundTrackBounds"
